<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-logo">
        <a href="#">
          <img src="../assets/pku.webp" alt="logo" style="width: 200px; height: 200px">
        </a>
      </div>
      <div class="login-form">
        <h3>用户登录</h3>
        <el-form ref="form" :model="loginForm" :rules="rules" label-position="left" label-width="0">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <div class="button-container">
              <el-button type="primary" @click="do_login" :loading="loading">登录</el-button>
              <div class="spacer"></div>
              <el-button type="success" @click="do_sign" :loading="loading">注册</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {login} from '../api/User_Login'
import {setACCESSToken, setRefreshToken } from "@/utils/auth";
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    do_login() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          // 发请求
          login(this.loginForm.username,this.loginForm.password)
              .then(res =>{
                if(res.code == 50007) {
                  this.$message.error('用户名或密码错误');
                  this.loading = false;
                }
                else{
                  this.$message.success("登录成功");
                  setACCESSToken(res.data.accessToken);
                  setRefreshToken(res.data.refreshToken)
                  this.$router.push({ path: '/User' })
                }
          }).catch(() => {
            // 登录失败，显示错误提示
            this.$message.error('用户名或密码错误')
            this.loading = false
          })
        }
      })
    }
  }
}
</script>


<style lang="scss">
.button-container {
  display: flex;
  align-items: center;
}

.spacer {
  flex-grow: 1;
  margin: 0 5px;
}

.login-container {
  height: 100vh; /* Use the full height of the viewport */
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 400px;
  height: auto; /* Auto height to accommodate content */
  border-radius: 5px;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); /* Improved box shadow */
  text-align: center; /* Center text */
}

.login-logo img {
  width: 200px;
  height: 200px;
  margin-bottom: 20px; /* Add margin below the image */
}

.login-form h3 {
  margin-bottom: 20px; /* Add margin below the heading */
}
</style>

